/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 10592
 */
public class Pager implements Serializable{
    private long rowCount;//总的数据条数
    private int rowPerPage; //每页显示的数据条数
    private int currentPage; //当前页的页码
    private int PageCount; //总的页数
    private int PagePerDisplay; //页码控件显示的页面按钮的个数
    private int displayCount;           // 
    private Integer[] displayPages;

    public Pager(long rowCount, int currentPage) {
        this(rowCount,5,currentPage,5);
    }

    public Pager(long rowCount, int rowPerPage, int currentPage) {
        this(rowCount, rowPerPage, currentPage, 5);
    }


    public Pager(long rowCount, int rowPerPage, int currentPage, int PagePerDisplay) {
        super();
        this.rowCount = rowCount;
        this.rowPerPage = rowPerPage;
        this.currentPage = currentPage;
        this.PagePerDisplay = PagePerDisplay;
//        System.out.println("this.rowCount:" + this.rowCount + "\t" + "rowPerPage:" + rowPerPage);
//        System.out.println("this.currentPage:" + this.currentPage + "\t" + "PagePerDisplay:" + PagePerDisplay);

        this.PageCount=(int)Math.ceil((float)rowCount/rowPerPage);
        this.displayCount=(int)Math.ceil((float)PageCount/this.PagePerDisplay);
//        System.out.println("this.PageCount:"+this.PageCount+"\t"+"displayCount"+displayCount);

        initDisplayPages();
//        System.out.println("this.PageCount:" + this.PageCount + "\t" + "displayCount" + displayCount);

    }

    public long getRowCount() {
        return rowCount;
    }

    public void setRowCount(long rowCount) {
        this.rowCount = rowCount;
    }

    public int getRowPerPage() {
        return rowPerPage;
    }

    public void setRowPerPage(int rowPerPage) {
        this.rowPerPage = rowPerPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageCount() {
        return PageCount;
    }

    public void setPageCount(int PageCount) {
        this.PageCount = PageCount;
    }

    public int getPagePerDisplay() {
        return PagePerDisplay;
    }

    public void setPagePerDisplay(int PagePerDisplay) {
        this.PagePerDisplay = PagePerDisplay;
    }

    public int getDisplayCount() {
        return displayCount;
    }

    public void setDisplayCount(int displayCount) {
        this.displayCount = displayCount;
    }

    public Integer[] getDisplayPages() {
        return displayPages;
    }

    public void setDisplayPages(Integer[] displayPages) {
        this.displayPages = displayPages;
    }

    private void initDisplayPages() {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        int currentDisplayNumber=(int)Math.ceil((float)this.currentPage/PagePerDisplay);
        List<Integer> pages = new ArrayList<Integer>();
        for (int i = ((currentDisplayNumber - 1) * PagePerDisplay + 1); i <= currentDisplayNumber
                * PagePerDisplay; i++) {
            if (((i - 1) * this.rowPerPage + 1) <= this.getRowCount()) {
                pages.add(i);
            }
        }
        this.displayPages = (Integer[]) pages.toArray(new Integer[0]);

    }

    public static void main(String[] args) {
       
        Pager pager = new Pager(9,5,1);
    }
    
    
}
